package game_engine.core;


import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;


public class Collision {

	private EntityGroup col1;
	private EntityGroup col2;
	private int destroy;
	public static final int BOTH = 0;
	public static final int COL1 = 1;
	public static final int COL2 = 2;
	
	public Collision(EntityGroup col1, EntityGroup col2, int destroy) {
		this.col1 = col1;
		this.col2 = col2;
		this.destroy = destroy;
	}
	
	public EntityGroup getCol1() {
		return col1;
	}
	
	public EntityGroup getCol2() {
		return col2;
	}
	
	public int getDestroy() {
		return destroy;
	}
	
	public void checkCollision() {
		synchronized(col1.getEntities()) {
			for(int i = 0 ; i< col1.getEntities().size() ; i++) {
				synchronized(col2.getEntities()) {
					for(int j = 0 ; j < col2.getEntities().size() ; j++) {
						Rectangle2D box1 = new Rectangle((int)Math.round(col1.getEntities().get(i).getBox().getBounds().x + col1.getEntities().get(i).getPosition().x), (int)Math.round(col1.getEntities().get(i).getBox().getBounds().y + col1.getEntities().get(i).getPosition().y), col1.getEntities().get(i).getBox().getBounds().width, col1.getEntities().get(i).getBox().getBounds().height);
						Rectangle2D box2 = new Rectangle((int)Math.round(col2.getEntities().get(j).getBox().getBounds().x + col2.getEntities().get(j).getPosition().x), (int)Math.round(col2.getEntities().get(j).getBox().getBounds().y + col2.getEntities().get(j).getPosition().y), col2.getEntities().get(j).getBox().getBounds().width, col2.getEntities().get(j).getBox().getBounds().height);
						if (!(box2.getMinX() > box1.getMaxX() ||  box2.getMaxX() < box1.getMinX() || box2.getMinY() > box1.getMaxY() || box2.getMaxY() < box1.getMinY()) ) {
							if(destroy==Collision.COL1)col1.getEntities().get(i).collided(col2.getEntities().get(j));
							else if(destroy==Collision.COL2)col2.getEntities().get(j).collided(col1.getEntities().get(i));
							else if(destroy==Collision.BOTH){
								col1.getEntities().get(i).collided(col2.getEntities().get(j));
								col2.getEntities().get(j).collided(col1.getEntities().get(i));
							}
						}
					}
				}
			}
		}
	}
	
}
